//
// Created by Soar Qin on 2017/8/22.
// Copyright (c) 2017 Soar Qin. All rights reserved.
//

#pragma once

struct sockaddr;

namespace core::async {

class IPAddr {
    friend struct IPAddrHash;
public:
    inline IPAddr() = default;
    explicit IPAddr(const std::string& address);
    IPAddr(const std::string& ip, uint16_t port);

    bool operator==(const IPAddr &other) const;

    inline bool ok() const { return ok_; }
    inline bool isIPv6() const { return isv6_; }
    const struct sockaddr *getAddr(int &size) const;
    std::tuple<struct sockaddr *, int> mutableAddr();

    void set(const struct sockaddr *addr);

private:
    bool ok_ = false;
    bool isv6_ = false;
    char addr_[128] {};
};

struct IPAddrHash {
    size_t operator()(const IPAddr &s) const;
};

}
